<?php

namespace App\Services\Flight;

use App\Repositories\Contracts\FlightDynamicInterface;
use Curl\CurlService\CurlService;
use Illuminate\Http\Request;

class FlightSearchService
{
    /**
     * 航班动态筛选条件
     * @param  $request
     * @return mixed
     */
    public function searchFlightDynamic($request){

        $where = [];
        if ( $request->id) {

            $where['flightId'] = $request->id;
        }
        if ($request->airline) {

            $where['airline'] = $request->airline;
        }
        if ($request->flight_code) {

            $where['flightCode'] = $request->flight_code;
        }
        if ($request->depart_code){

            $where['depCode'] = $request->depart_code;
        }
        if ($request->arrived_code){

            $where['arrCode'] = $request->arrived_code;
        }
        if ($request->start_at){
            $where['beginTime'] = $request->start_at;
        }
        if ($request->day_num){
            $where['dayNum'] = $request->day_num;
        }
        if ($request->weather){

            $where['weatherList'] = explode(',',$request->weather);
        }

        /**
         * 排序
         */
        if ($request->order_field){

            $orderFields = explode(',',$request->order_field);
            foreach ($orderFields as $field) {
                $sort = explode('/',$field);
                $where[$sort[0]] = $sort[1];

            }
        }

        $whereIn = [];
        if ($where) {

            $dynamicRes = CurlService::post(env('FLIGHT_DYNAMIC_URL').'/flightDynamic/queryLxGoOrDown',$where);
            if ($dynamicRes && !empty($dynamicRes['data'])){

                $whereIn = $dynamicRes['data'];
            } else {
                $whereIn[] = -1 ;
            }
        }

       return $whereIn;
    }
}